﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ShowWindow
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            if (0 == args.Length)
            {
                Console.WriteLine("Please input language");
            }
            else if (1 == args.Length)
            {
                if (IsExist(args[0]))
                {
                    SetlocalSilverlightSetup(args[0].ToLower());
                }
                else
                {
                    Console.WriteLine("Please add the local string from install.res.dll String Table[language]");
                }
            }
            if (localSilverlightSetup.Length != 0)
            {
                User32.ShowWindow(FindChildHandleByIndex(FindParentHandle(), 2), 5);
                Console.WriteLine("Screen shot quickly!");
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("Occer error, please contract Bing Zhao");
            }
        }

        public static string localSilverlightSetup;
        public static Boolean IsExist(string language)
        {
            Boolean mark = false;
            XmlNodeList nodeList = GetNodeList();
            foreach (XmlNode currenctNode in nodeList)
            {
                if (currenctNode.Name.ToLower() == language)
                {
                    mark = true;
                }
            }
            return mark;
        }

        private static XmlNodeList GetNodeList()
        {
            XmlNode node = GetXmlDocument().DocumentElement;
            XmlNodeList nodeList = node.ChildNodes;
            return nodeList;
        }
        public static XmlDocument GetXmlDocument()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("SilverlightSetup.xml");
            return xmlDoc;
        }
        public static void SetlocalSilverlightSetup(string language)
        {
            XmlNodeList nodeList = GetNodeList();
            foreach (XmlNode currenctNode in nodeList)
            {
                if (currenctNode.Name.ToLower() == language)
                {
                    localSilverlightSetup = currenctNode.InnerText;
                }
            }
        }

        public static IntPtr FindParentHandle()
        {
            return User32.FindWindow(null, localSilverlightSetup);
        }

        public static IntPtr FindChildHandleByIndex(IntPtr parentHandle, int index)
        {
            if (index == 0)
            {
                return IntPtr.Zero;
            }
            else
            {
                int ct = 0;
                IntPtr result = IntPtr.Zero;
                do
                {
                    result = User32.FindChildHandleEx(parentHandle, result, null, null);
                    if (result != IntPtr.Zero)
                    {
                        ++ct;
                    }
                }
                while (ct < index && result != IntPtr.Zero);
                return result;
            }
        }
    }
}
